package com.neusoft.aclome.cmp.cloud.server.jcce.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.neusoft.aclome.cmp.cloud.server.jcce.constants.DataResourceFileType;
import com.neusoft.aclome.cmp.cloud.server.jcce.constants.ProprietorshipStatusEnum;
import com.neusoft.aclome.cmp.cloud.server.jcce.constants.ResourceApplyStatusEnum;
import com.neusoft.aclome.cmp.cloud.server.jcce.dto.response.DataResourceDTO;
import com.neusoft.aclome.cmp.cloud.server.jcce.dto.response.UserResourceApplyDTO;
import com.neusoft.aclome.cmp.cloud.server.jcce.entity.DataResourceDictEntity;
import com.neusoft.aclome.cmp.cloud.server.jcce.entity.DataResourceEntity;
import com.neusoft.aclome.cmp.cloud.server.jcce.entity.DataResourceOwnershipEntity;
import com.neusoft.aclome.cmp.cloud.server.jcce.entity.UserResourceApplyEntity;
import com.neusoft.aclome.cmp.cloud.server.jcce.repository.DataResourceDictMapper;
import com.neusoft.aclome.cmp.cloud.server.jcce.repository.DataResourceMapper;
import com.neusoft.aclome.cmp.cloud.server.jcce.repository.DataResourceOwnershipMapper;
import com.neusoft.aclome.cmp.cloud.server.jcce.repository.UserResourceApplyMapper;
import com.neusoft.aclome.cmp.cloud.server.pub.cloud.exception.AclomeCloudPlatformException;
import com.neusoft.aclome.cmp.cloud.server.pub.cloud.exception.AclomeCloudPlatformExceptionErrorMsg;
import com.neusoft.aclome.cmp.cloud.server.service.AdminServerService;
import com.neusoft.aclome.cmp.cloud.server.service.ToolsService;
import com.neusoft.aclome.cmp.common.model.vo.HttpResponse;
import com.neusoft.aclome.cmp.common.model.vo.ResponsePage;
import com.neusoft.aclome.cmp.common.utils.StringUtils;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.util.Asserts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * describe :
 *
 * @author hao.dd
 * @date 2023/11/27
 */
@Service
public class DataResourceService {

    private static final Logger log = LoggerFactory.getLogger(DataResourceService.class);

    @Autowired
    private DataResourceMapper dataResourceMapper;

    @Autowired
    private DataResourceOwnershipMapper dataResourceOwnershipMapper;

    @Autowired
    private DataResourceDictMapper dataResourceDictMapper;
    @Autowired
    private ToolsService toolsService;

    @Autowired
    private AdminServerService adminServerService;

    @Autowired
    private UserResourceApplyMapper userResourceApplyMapper;
    private static final String bucketName = "data-resource";

    public void saveDataResource(String dataResourceName, String status, String dataFileName,
                                 String newDataFileName, String proofFileName,
                                 String newProofFileName, String newImageFileName,
                                 String userId,
                                 double dataFileSize, BigDecimal price, String dataType,
                                 String dataCategory, String proofType, String description,String usageRightDetail) {
        DataResourceEntity dataResourceEntity = new DataResourceEntity();
        dataResourceEntity.setDataFileName(dataFileName);
        dataResourceEntity.setProofFileName(proofFileName);
        dataResourceEntity.setProprietorshipStatus(status);
        dataResourceEntity.setName(dataResourceName);
        dataResourceEntity.setUsageRightDetail(usageRightDetail);
        Date date = new Date();
        dataResourceEntity.setUpdatedAt(date);
        dataResourceEntity.setCreatedAt(date);
        dataResourceEntity.setNewDataFileName(newDataFileName);
        dataResourceEntity.setNewProofFileName(newProofFileName);

        // dataResourceEntity.setNewProofDesc(newProofDesc);
        // dataResourceEntity.setProofDesc(proofDesc);
        // dataResourceEntity.setNewOwnershipDesc(newOwnershipDesc);
        // dataResourceEntity.setOwnershipDesc(ownershipDesc);

        dataResourceEntity.setUserId(userId);
        dataResourceEntity.setDataCategory(dataCategory);

        JSONObject userInfoJsonObject = adminServerService.info(userId).getData();
        dataResourceEntity.setDataSupplier(userInfoJsonObject.getString("name"));

        dataResourceEntity.setDataFormat(".".concat(FilenameUtils.getExtension(newDataFileName)));
        dataResourceEntity.setDataSize(dataFileSize + "MB");
        dataResourceEntity.setDataType(dataType);
        dataResourceEntity.setDataCategory(dataCategory);
        dataResourceEntity.setImageName(newImageFileName);
        dataResourceEntity.setPrice(price);
        dataResourceEntity.setDescription(description);
        dataResourceEntity.setRegistrationTime(date);
        dataResourceEntity.setProofType(proofType);
        dataResourceEntity.setUsageRightDetail(usageRightDetail);

        dataResourceMapper.insert(dataResourceEntity);
    }

    public HttpResponse auditDataResource(Integer id, ProprietorshipStatusEnum proprietorshipStatus) {

        DataResourceEntity dataResourceEntity = dataResourceMapper.selectByPrimaryKey(id);
        if(dataResourceEntity.getProofFileDownloadStatus()==null && "APPROVED".equals(proprietorshipStatus.name())){
            return HttpResponse.resultError(500,"请先下载证明文件并阅读完毕");
        }
        if (dataResourceEntity != null) {
            if (ProprietorshipStatusEnum.APPROVED.equals(proprietorshipStatus)) {
                // 下载文件 并进行签名，保存签名结果
                byte[] bytes = toolsService.downloadFileNew(bucketName, dataResourceEntity.getNewDataFileName()).getBody();
                if (bytes != null) {
                    String signature = signature(bytes);
                    dataResourceEntity.setDataFileSignature(signature);
                }
            }
            dataResourceEntity.setProprietorshipStatus(proprietorshipStatus.name());
            dataResourceEntity.setUpdatedAt(new Date());
            dataResourceMapper.updateByPrimaryKey(dataResourceEntity);
        } else {
            throw new IllegalArgumentException("不存在此数据资源，请核对");
        }
        return HttpResponse.resultSuccess();
    }

    public ResponsePage<DataResourceDTO> listDataResource(Integer pageNo, Integer pageSize,
                                                          ProprietorshipStatusEnum proprietorshipStatus,
                                                          ResourceApplyStatusEnum resourceApplyStatusEnum,
                                                          String category,
                                                          String type,
                                                          Long userId) {
        // 创建分页对象
        PageHelper.startPage(pageNo, pageSize);

        String proprietorshipStatusStr = null;
        if (proprietorshipStatus != null) {
            proprietorshipStatusStr = proprietorshipStatus.name();
        }

        String resourceApplyStatusEnumStr = null;
        if (resourceApplyStatusEnum != null) {
            resourceApplyStatusEnumStr = resourceApplyStatusEnum.name();
        }
        List<DataResourceDTO> dataResourceList = dataResourceMapper.getDataResourceList(proprietorshipStatusStr,
                resourceApplyStatusEnumStr, category, type, userId);
        List<DataResourceOwnershipEntity> ownershipEntities = dataResourceOwnershipMapper.selectAll();

        Map<Integer, List<DataResourceOwnershipEntity>> ownershipEntityMap =  new HashMap<>();
        if(CollectionUtils.isNotEmpty(ownershipEntities)){
            ownershipEntityMap = ownershipEntities.stream().collect(Collectors.groupingBy(x -> x.getDataResourceId(),Collectors.toList()));
        }
        List<DataResourceDictEntity> dictEntities = dataResourceDictMapper.selectAll();
        Map<String, String> categoryMap = dictEntities.stream().filter(x -> x.getType().equals("category")).collect(Collectors.toMap(x -> x.getValue(), x -> x.getLocalName()));
        Map<String, String> typeMap = dictEntities.stream().filter(x -> x.getType().equals("type")).collect(Collectors.toMap(x -> x.getValue(), x -> x.getLocalName()));
        Map<String, String> proofTypeMap = dictEntities.stream().filter(x -> x.getType().equals("proofType")).collect(Collectors.toMap(x -> x.getValue(), x -> x.getLocalName()));


        PageInfo<DataResourceDTO> pageInfo = new PageInfo<>(dataResourceList);
        for (DataResourceDTO dataResourceDTO : pageInfo.getList()) {
            if (dataResourceDTO.getResourceApplyStatus() != null) {
                dataResourceDTO.setResourceApplyStatusLocalName(dataResourceDTO.getResourceApplyStatus().getLocalName());
            } else {
                dataResourceDTO.setResourceApplyStatus(ResourceApplyStatusEnum.NOT_SUBMITTED);
                dataResourceDTO.setResourceApplyStatusLocalName(ResourceApplyStatusEnum.NOT_SUBMITTED.getLocalName());
            }
            dataResourceDTO.setProprietorshipStatusLocalName(dataResourceDTO.getProprietorshipStatus().getLocalName());
            dataResourceDTO.setDataType(typeMap.get(dataResourceDTO.getDataType()));
            dataResourceDTO.setDataCategory(categoryMap.get(dataResourceDTO.getDataCategory()));
            dataResourceDTO.setProofType(proofTypeMap.get(dataResourceDTO.getProofType()));
            // 设置权属信息
            List<DataResourceOwnershipEntity> dataResourceOwnershipEntitys = ownershipEntityMap.get(dataResourceDTO.getId());
            if(dataResourceOwnershipEntitys!=null){
                dataResourceDTO.setOwnerships((JSONArray) JSONArray.toJSON(dataResourceOwnershipEntitys));
            }
        }
        ResponsePage<DataResourceDTO> jcceJobEntityResponsePage = new ResponsePage<DataResourceDTO>();
        jcceJobEntityResponsePage.setSize(pageInfo.getSize());
        jcceJobEntityResponsePage.setPages(pageInfo.getPages());
        jcceJobEntityResponsePage.setPage(pageNo);
        jcceJobEntityResponsePage.setTotal(pageInfo.getTotal());
        DataResourceSorter.sortDataResourceList(pageInfo.getList());
        jcceJobEntityResponsePage.setPageData(pageInfo.getList());
        return jcceJobEntityResponsePage;
    }

    /**
     * 数据共享用，没有交易相关的流程
     */
    public ResponsePage<DataResourceDTO> listDataShareResource(Integer pageNo, Integer pageSize, ProprietorshipStatusEnum proprietorshipStatus, String category, String type) {
        // 创建分页对象
        PageHelper.startPage(pageNo, pageSize);

        String proprietorshipStatusStr = null;
        if (proprietorshipStatus != null) {
            proprietorshipStatusStr = proprietorshipStatus.name();
        }

        List<DataResourceDTO> dataResourceList = dataResourceMapper.getDataShareResourceList(proprietorshipStatusStr, category, type);
        List<DataResourceDictEntity> dictEntities = dataResourceDictMapper.selectAll();

        Map<String, String> categoryMap = dictEntities.stream().filter(x -> x.getType().equals("category")).collect(Collectors.toMap(x -> x.getValue(), x -> x.getLocalName()));
        Map<String, String> typeMap = dictEntities.stream().filter(x -> x.getType().equals("type")).collect(Collectors.toMap(x -> x.getValue(), x -> x.getLocalName()));

        PageInfo<DataResourceDTO> pageInfo = new PageInfo<>(dataResourceList);
        for (DataResourceDTO dataResourceDTO : pageInfo.getList()) {
            dataResourceDTO.setProprietorshipStatusLocalName(dataResourceDTO.getProprietorshipStatus().getLocalName());
            dataResourceDTO.setDataType(typeMap.get(dataResourceDTO.getDataType()));
            dataResourceDTO.setDataCategory(categoryMap.get(dataResourceDTO.getDataCategory()));
        }

        ResponsePage<DataResourceDTO> jcceJobEntityResponsePage = new ResponsePage<DataResourceDTO>();
        jcceJobEntityResponsePage.setSize(pageInfo.getSize());
        jcceJobEntityResponsePage.setPages(pageInfo.getPages());
        jcceJobEntityResponsePage.setPage(pageNo);
        jcceJobEntityResponsePage.setTotal(pageInfo.getTotal());
        jcceJobEntityResponsePage.setPageData(pageInfo.getList());
        return jcceJobEntityResponsePage;
    }

    public void downloadFile(Integer id, DataResourceFileType fileType, String bucketName, HttpServletResponse response) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        DataResourceEntity dataResourceEntity = dataResourceMapper.selectByPrimaryKey(id);

        if (dataResourceEntity != null) {
            ResponseEntity<byte[]> responseEntity = null;
            if (DataResourceFileType.DATA_FILE.equals(fileType)) {
                responseEntity = toolsService.downloadFileNew(bucketName, dataResourceEntity.getNewDataFileName());
            } else if (DataResourceFileType.PROOF_FILE.equals(fileType)) {
                responseEntity = toolsService.downloadFileNew(bucketName, dataResourceEntity.getNewProofFileName());
                dataResourceEntity.setProofFileDownloadStatus("downloaded");
                dataResourceMapper.updateByPrimaryKey(dataResourceEntity);
            } else if (DataResourceFileType.IMAGE_FILE.equals(fileType)) {
                responseEntity = toolsService.downloadFileNew(bucketName, dataResourceEntity.getImageName());
            } else if (DataResourceFileType.PROOF_DESC_FILE.equals(fileType)) {
                log.info("----------------------PROOF_DESC_FILE------------------------");
                responseEntity = toolsService.downloadFileNew(bucketName, dataResourceEntity.getProofDesc());
                try {
                    PdfReader reader = new PdfReader(responseEntity.getBody());
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    PdfStamper stamper = new PdfStamper(reader, byteArrayOutputStream);
                    int pageCount = reader.getNumberOfPages();
                    // 添加水印
                    for (int i = 1; i <= pageCount; i++) {
                        PdfContentByte contentByte = stamper.getUnderContent(i); // 或者 getOverContent()
                        contentByte.beginText();
                        contentByte.setFontAndSize(BaseFont.createFont(), 36f);
                        contentByte.setColorFill(BaseColor.LIGHT_GRAY);
                        contentByte.showTextAligned(Element.ALIGN_CENTER, "JointCloud " + sdf.format(new Date()), 300, 400, 45);
                        contentByte.endText();
                    }
                    stamper.close();
                    reader.close();
                    byte[] pdfBytes = byteArrayOutputStream.toByteArray();
                    int contentLength = pdfBytes.length;
                    OutputStream outputStream = response.getOutputStream();

                    response.setContentLength(contentLength);
                    response.setContentType(responseEntity.getHeaders().getContentType().toString());
                    response.setHeader("Content-Disposition", responseEntity.getHeaders().getFirst("Content-Disposition"));
                    outputStream.write(pdfBytes);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (DataResourceFileType.OWNERSHIP_DESC_FILE.equals(fileType)) {
                log.info("----------------------OWNERSHIP_DESC_FILE------------------------");
                responseEntity = toolsService.downloadFileNew(bucketName, dataResourceEntity.getOwnershipDesc());
                try {
                    PdfReader reader = new PdfReader(responseEntity.getBody());
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    PdfStamper stamper = new PdfStamper(reader, byteArrayOutputStream);
                    int pageCount = reader.getNumberOfPages();
                    // 添加水印
                    for (int i = 1; i <= pageCount; i++) {
                        PdfContentByte contentByte = stamper.getUnderContent(i); // 或者 getOverContent()
                        contentByte.beginText();
                        contentByte.setFontAndSize(BaseFont.createFont(), 36f);
                        contentByte.setColorFill(BaseColor.LIGHT_GRAY);
                        contentByte.showTextAligned(Element.ALIGN_CENTER, "JointCloud " + sdf.format(new Date()), 300, 400, 45);
                        contentByte.endText();
                    }
                    stamper.close();
                    reader.close();
                    byte[] pdfBytes = byteArrayOutputStream.toByteArray();
                    int contentLength = pdfBytes.length;
                    OutputStream outputStream = response.getOutputStream();

                    response.setContentLength(contentLength);
                    response.setContentType(responseEntity.getHeaders().getContentType().toString());
                    response.setHeader("Content-Disposition", responseEntity.getHeaders().getFirst("Content-Disposition"));
                    outputStream.write(pdfBytes);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (DataResourceFileType.IMAGE_FILE.equals(fileType)
                    || DataResourceFileType.DATA_FILE.equals(fileType)
                    || DataResourceFileType.PROOF_FILE.equals(fileType)) {
                //使用 IOUtils 类的 copy 方法将 材积服务ResponseEntity 对象写入到 HttpServletResponse 对象的输出流中。
                response.setContentType(responseEntity.getHeaders().getContentType().toString());
                response.setHeader("Content-Disposition", responseEntity.getHeaders().getFirst("Content-Disposition"));
                response.setContentLength((int) responseEntity.getHeaders().getContentLength());
                IOUtils.copy(new ByteArrayInputStream(responseEntity.getBody()), response.getOutputStream());
            }

            response.getOutputStream().flush();
        }
    }

    /**
     * 对二进制文件进行签名
     * 并返回16进制字符串
     *
     * @param bytes
     * @return
     */
    private String signature(byte[] bytes) {
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }

        digest.update(bytes);
        byte[] hashedBytes = digest.digest();

        StringBuilder hexString = new StringBuilder();
        for (byte b : hashedBytes) {
            String hex = Integer.toHexString(0xFF & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }

        return hexString.toString();
    }

    public List<DataResourceDictEntity> listDataResourceDict(String dictType) {
        Example systemExample = new Example(DataResourceDictEntity.class);
        Example.Criteria criteria = systemExample.createCriteria();
        if (dictType != null) {
            criteria.andEqualTo("type", dictType);
        }
        return dataResourceDictMapper.selectByExample(systemExample);
    }

    @Transactional(rollbackFor = Exception.class)
    public void applyUserResourceApplication(Integer resourceId, Long userId, String applyNote,String usageRightDetail,Date startTime,Date endTime) {
        // TODO 添加已确权才能够进行申请的check  check dataResource是否存在
        Example systemExample = new Example(UserResourceApplyEntity.class);
        Example.Criteria criteria = systemExample.createCriteria();
        criteria.andEqualTo("resourceId", resourceId);
        criteria.andEqualTo("userId", userId);
        UserResourceApplyEntity userResourceApply = userResourceApplyMapper.selectOneByExample(systemExample);

        if (userResourceApply != null) {
            if (userResourceApply.getAuditStatus().equals(ResourceApplyStatusEnum.REJECTED.name())) {
                userResourceApplyMapper.delete(userResourceApply);
            } else {
                throw new AclomeCloudPlatformException(
                        new AclomeCloudPlatformExceptionErrorMsg(
                                "当前申请已存在，不可重新申请"), null);
            }
        }

        JSONObject data = adminServerService.info(String.valueOf(userId)).getData();
        Asserts.notNull(data, "用户数据");
        UserResourceApplyEntity applyEntity = new UserResourceApplyEntity();
        applyEntity.setResourceId(resourceId);
        applyEntity.setApplyTime(new Date());
        applyEntity.setAuditStatus(ResourceApplyStatusEnum.SUBMITTED.name());
        applyEntity.setUserId(userId);
        applyEntity.setApplyNote(applyNote);
        applyEntity.setUsageRightDetail(usageRightDetail);
        applyEntity.setStartTime(startTime);
        applyEntity.setEndTime(endTime);
        userResourceApplyMapper.insert(applyEntity);
    }

    public void auditUserResourceApply(Integer resourceId, Long userId, Boolean status) {
        Example systemExample = new Example(UserResourceApplyEntity.class);
        Example.Criteria criteria = systemExample.createCriteria();
        criteria.andEqualTo("resourceId", resourceId);
        criteria.andEqualTo("userId", userId);
        UserResourceApplyEntity userResourceApply = userResourceApplyMapper.selectOneByExample(systemExample);
        Asserts.notNull(userResourceApply, "用户申请数据");

        if (!userResourceApply.getAuditStatus().equals(ResourceApplyStatusEnum.SUBMITTED.name())) {
            throw new IllegalArgumentException("当前审核数据有误或已审核，请核对");
        }

        if (status) {
            userResourceApply.setAuditStatus(ResourceApplyStatusEnum.AUDIT_APPROVE.name());
        } else {
            userResourceApply.setAuditStatus(ResourceApplyStatusEnum.REJECTED.name());
        }
        userResourceApply.setAuditTime(new Date());


        userResourceApplyMapper.updateByPrimaryKey(userResourceApply);
    }

    public void platformAuditUserResourceApply(Integer resourceId, Long userId) {


        Example systemExample = new Example(UserResourceApplyEntity.class);
        Example.Criteria criteria = systemExample.createCriteria();
        criteria.andEqualTo("resourceId", resourceId);
        criteria.andEqualTo("userId", userId);
        UserResourceApplyEntity userResourceApply = userResourceApplyMapper.selectOneByExample(systemExample);
        Asserts.notNull(userResourceApply, "用户申请数据");

        if (!userResourceApply.getAuditStatus().equals(ResourceApplyStatusEnum.AUDIT_APPROVE.name())) {
            throw new IllegalArgumentException("当前审核数据有误或已审核，请核对");
        }
        userResourceApply.setAuditStatus(ResourceApplyStatusEnum.DOWNLOADABLE.name());
        userResourceApply.setAuditTime(new Date());

        userResourceApplyMapper.updateByPrimaryKey(userResourceApply);
    }

    public ResponsePage<UserResourceApplyDTO> userResourceApplyList(Integer pageNo, Integer pageSize, ResourceApplyStatusEnum resourceApplyStatusEnumStatus) {
        // 创建分页对象
        PageHelper.startPage(pageNo, pageSize);
        List<UserResourceApplyDTO> userResourceApplyDTOS = userResourceApplyMapper.userResourceApplyList(resourceApplyStatusEnumStatus != null ? resourceApplyStatusEnumStatus.name() : null);
        PageInfo<UserResourceApplyDTO> pageInfo = new PageInfo<>(userResourceApplyDTOS);
        ResponsePage<UserResourceApplyDTO> result = new ResponsePage<>();
        result.setPageData(pageInfo.getList());

        if (CollectionUtils.isNotEmpty(pageInfo.getList())) {
            pageInfo.getList().forEach(info -> {
                if (!StringUtils.isEmpty(info.getUserId())) {
                    JSONObject userInfoJsonObject = adminServerService.info(info.getUserId() + "").getData();
                    if (userInfoJsonObject != null) {
                        info.setUserShowName(userInfoJsonObject.getString("name"));
                    }
                }
            });
        }

        result.setSize(pageInfo.getSize());
        result.setPages(pageInfo.getPages());
        result.setPage(pageNo);
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    public DataResourceDTO dataResourceWithId(Integer resourceId) {
        DataResourceEntity dataResourceEntity = dataResourceMapper.selectByPrimaryKey(resourceId);
        DataResourceDTO dataResourceDTO = new DataResourceDTO();
        BeanUtils.copyProperties(dataResourceEntity, dataResourceDTO);
        dataResourceDTO.setProprietorshipStatus(ProprietorshipStatusEnum.valueOf(dataResourceEntity.getProprietorshipStatus()));
        dataResourceDTO.setProprietorshipStatusLocalName(ProprietorshipStatusEnum.valueOf(dataResourceEntity.getProprietorshipStatus()).getLocalName());
        return dataResourceDTO;
    }
}
